<?php

namespace addons\yunfood\model;

use addons\yunfood\library\Logic;
use addons\yunfood\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 轮播模型
 */
class Slide extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_slide';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_NORMAL);
    }

    // 门店名称
    public function getShopTextAttr($value, $data)
    {
        return Logic::getShopNames($data['shop_ids']);
    }

    // 图片
    public function getImageUrlAttr($value, $data)
    {
        return cdnurl($value, true);
    }

    // 位置
    const POSITION_HOME = 1;
    const POSITION_ORDER = 2;

    public static function getPositionList()
    {
        return [self::POSITION_HOME => __('首页'), self::POSITION_ORDER => __('点餐页')];
    }

    public function getPositionTextAttr($value, $data)
    {
        $optionList = self::getPositionList();

        return $optionList[$data['position']] ?? $data['position'];
    }

    // 跳转类型
    const REDIRECT_TYPE_LINK   = 'link';
    const REDIRECT_TYPE_PAGE   = 'page';
    const REDIRECT_TYPE_SOURCE = 'source';

    public static function getRedirectTypeList()
    {
        return [
            self::REDIRECT_TYPE_LINK   => __('外部链接'),
            self::REDIRECT_TYPE_PAGE   => __('指定页面'),
            self::REDIRECT_TYPE_SOURCE => __('内容资源')
        ];
    }

    public function getRedirectTypeTextAttr($value, $data)
    {
        $optionList = self::getRedirectTypeList();

        return $optionList[$data['redirect_type']] ?? $data['redirect_type'];
    }

    public function getRedirectDataAttr($value, $data)
    {
        return json_decode($data['redirect_data'], true);
    }

    // 链接数据
    public function getLinkDataAttr($value, $data)
    {
        $redirectData = json_decode($data['redirect_data'], true);

        return $data['redirect_type'] == self::REDIRECT_TYPE_LINK ? $redirectData : ['url' => ''];
    }

    // 页面数据
    public function getPageDataAttr($value, $data)
    {
        $redirectData = json_decode($data['redirect_data'], true);

        return $data['redirect_type'] == self::REDIRECT_TYPE_PAGE ? $redirectData : ['page' => ''];
    }

    // 资源数据
    public function getSourceDataAttr($value, $data)
    {
        $redirectData = json_decode($data['redirect_data'], true);

        return $data['redirect_type'] == self::REDIRECT_TYPE_SOURCE ? $redirectData : ['type' => '', 'model_id' => ''];
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('Normal'), self::STATUS_HIDDEN => __('屏蔽')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }
}
